package com.itheima.travel.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.travel.mapper.FavoriteMapper;
import com.itheima.travel.mapper.RouteMapper;
import com.itheima.travel.mapperExt.FavoriteMapperExt;
import com.itheima.travel.mapperExt.RouteMapperExt;
import com.itheima.travel.pojo.Favorite;
import com.itheima.travel.pojo.FavoriteExample;
import com.itheima.travel.pojo.Route;
import com.itheima.travel.pojo.RouteExample;
import com.itheima.travel.req.AffixVo;
import com.itheima.travel.req.FavoriteVo;
import com.itheima.travel.req.RouteVo;
import com.itheima.travel.service.AffixService;
import com.itheima.travel.service.FavoriteService;
import com.itheima.travel.session.SubjectUser;
import com.itheima.travel.session.SubjectUserContext;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FavoriteServiceImpl implements FavoriteService {
    @Autowired
    private FavoriteMapperExt favoriteMapperExt;

    @Autowired
    private FavoriteMapper favoriteMapper;

    @Autowired
    private RouteMapperExt routeMapperExt;

    @Autowired
    private RouteMapper routeMapper;

    @Autowired
    private SubjectUserContext subjectUserContext;

    @Autowired
    private AffixService affixService;

    @Override
    public PageInfo<RouteVo> findMyFavorite(FavoriteVo favoriteVo,int pageNum, int pageSize) {
//        使用分页工具
        PageHelper.startPage(pageNum,pageSize);
//        获取当前登录账户
        SubjectUser subject = subjectUserContext.getSubject();
//        //查询登录账户的收藏路线id
//        FavoriteExample favoriteExample = new FavoriteExample();
//        favoriteExample.createCriteria().andUserIdEqualTo(subject.getId());
//        List<Favorite> listF = favoriteMapper.selectByExample(favoriteExample);
//        //根据获取到的线路id获取路线信息
//        ArrayList<Route> list = new ArrayList<>();
//        for (Favorite favorite : listF) {
//            Route route = routeMapper.selectByPrimaryKey(favorite.getRouteId());
//            arrayList.add(route);
//        }
        //分页查询
        List<Route> list = favoriteMapperExt.findFavoriteByUserId(subject.getId());
        //封装查询结果
        PageInfo<Route> pageInfo = new PageInfo<>(list);
        PageInfo<RouteVo> pageInfoVo = new PageInfo<>();
        //拷贝对象
        BeanConv.toBean(pageInfo, pageInfoVo);
        List<RouteVo> routeVos = BeanConv.toBeanList(pageInfo.getList(), RouteVo.class);
        pageInfoVo.setList(routeVos);
        //处理图片问题
        for (RouteVo routeVo : routeVos) {
            AffixVo affixVo = AffixVo.builder().businessId(routeVo.getId()).build();
            routeVo.setAffixVoList(affixService.findAffixByBusinessId(affixVo));
        }
        return pageInfoVo;
    }

    @Override
    public Integer addFavorite(FavoriteVo favoriteVo) {

        SubjectUser subject = subjectUserContext.getSubject();
        if(EmptyUtil.isNullOrEmpty(subject)){
            return 0;
        }
        favoriteVo.setUserId(subject.getId());
//        Route route = routeMapper.selectByPrimaryKey(favoriteVo.getRouteId());
//        Favorite favorite = BeanConv.toBean(favoriteVo, Favorite.class);
//        int i = favoriteMapper.insert(favorite);
//        if(i>0){
//            route.setAttentionCount(route.getAttentionCount()+1);
//            int key = routeMapper.updateByPrimaryKey(route);
//            if(key>0){
//                return routeMapper.selectByPrimaryKey(favoriteVo.getRouteId()).getAttentionCount();
//            }
//        }
        favoriteMapper.insert(BeanConv.toBean(favoriteVo,Favorite.class));
        Long flag = routeMapperExt.updateRouteCountById(favoriteVo.getRouteId());
        if(flag==0){
            throw new RuntimeException("修改收藏次数出错");
        }
        Route route = routeMapper.selectByPrimaryKey(favoriteVo.getRouteId());
        return route.getAttentionCount();
    }

    @Override
    public Boolean isFavorited(FavoriteVo favoriteVo) {
        if(!subjectUserContext.existSubject()){
            return false;
        }
        SubjectUser subject = subjectUserContext.getSubject();
//        if(EmptyUtil.isNullOrEmpty(subject)){
//            return false;
//        }
        favoriteVo.setUserId(subject.getId());
        FavoriteExample favoriteExample = new FavoriteExample();
        favoriteExample.createCriteria().andUserIdEqualTo(favoriteVo.getUserId())
                .andRouteIdEqualTo(favoriteVo.getRouteId());
        List<Favorite> list = favoriteMapper.selectByExample(favoriteExample);
        if(list.size()>0){
            return true;
        }
        return false;
    }
}
